<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>物业管理平台登陆</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/login.css">
<style>
body {
	background: url(${pageContext.request.contextPath}/img/admin_login_bg.png) no-repeat;
	background-size: 100% 100%
}

.form {
	background: url(${pageContext.request.contextPath}/img/admin_login_form.png) no-repeat;
	background-size: 100% 100%;
}
</style>
</head>
<body>
	<div class="wrapper">
		<div class="form">
			<div class="input">
				<input id="username" type="text" placeholder="用户名">
			</div>
			<div class="input">
				<input id="password" type="password" placeholder="密码">
			</div>
			<div class="checkbox">
				<input type="checkbox" id="check"> <span>记住密码</span>
			</div>
			<div id="submit" class="btn" onclick="login();">
				<span>登陆</span>
			</div>
		</div>
	</div>
</body>
<script src="${pageContext.request.contextPath}/plugins/jQuery/jquery-2.2.3.min.js"></script>
<!-- layer -->
<script src="${pageContext.request.contextPath}/plugins/layer/2.4/layer.js"></script>
<script>
	$(function() {
		if (getCookie('username') && getCookie('password')) {
			$("#username").val(getCookie('username'));
			$("#password").val(getCookie('password'));
			document.getElementById('check').checked = true;
		}
	});
	function login() {
		var username = $('#username').val();
		if (username == "") {
			layer.msg('请输入用户名!', {
				icon : 5,
				time : 1000
			});
			return;
		}
		var password = $('#password').val();
		if (password == "") {
			layer.msg('请输入密码!', {
				icon : 5,
				time : 1000
			});
			return;
		}
		$.ajax({
			url : '${pageContext.request.contextPath}/user_login',
			type : 'post',
			data : {
				username : username,
				password : password
			},
			success : function(map) {
				console.log(map);
				var result = map['result'];
				if (result == 1) {
					layer.msg('登录成功!', {
						icon : 1,
						time : 1500
					});
					setTimeout(function() {
						if ($("#check").prop('checked')) {
							setCookie('username', username, 7); //保存帐号到cookie，有效期7天
							setCookie('password', password, 7); //保存密码到cookie，有效期7天
						}
						location.href = "jump_index";
					}, 800);
				} else if (result == 2) {
					layer.msg('密码错误!', {
						icon : 5,
						time : 1500
					});
				} else if (result == 0) {
					layer.msg('账号不存在!', {
						icon : 5,
						time : 1500
					});
				} else if (result == -1) {
					layer.msg('账号已冻结，请联系管理员!', {
						icon : 5,
						time : 1500
					});
				} else if (result == -2) {
					layer.msg('社区已冻结，请联系管理员!', {
						icon : 5,
						time : 1500
					});
				} else if (result == 3) {
					layer.msg('账号没有绑定社区，请联系管理员!', {
						icon : 5,
						time : 1500
					});
				} else {
					layer.msg('登录失败!', {
						icon : 2,
						time : 1500
					});
				}

			}
		});
	}
	$("body").keydown(function() {
		if (event.keyCode == "13") {//keyCode=13是回车键
			$('#submit').click();
		}
	});
	//设置cookie
	function setCookie(name, value, day) {
		var date = new Date();
		date.setDate(date.getDate() + day);
		document.cookie = name + '=' + value + ';expires=' + date;
	};
	//获取cookie
	function getCookie(name) {
		var reg = RegExp(name + '=([^;]+)');
		var arr = document.cookie.match(reg);
		if (arr) {
			return arr[1];
		} else {
			return '';
		}
	};
	//删除cookie
	function delCookie(name) {
		setCookie(name, null, -1);
	};
</script>
</html>
